<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业id" prop="corpId">
        <el-input
          v-model="queryParams.corpId"
          placeholder="请输入企业id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产编号(GZ_当前时间戳)" prop="assetNo">
        <el-input
          v-model="queryParams.assetNo"
          placeholder="请输入资产编号(GZ_当前时间戳)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用部门" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入使用部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购入时间" prop="assetBuyDate">
        <el-date-picker clearable
          v-model="queryParams.assetBuyDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择购入时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="资产原值" prop="initialAssetValue">
        <el-input
          v-model="queryParams.initialAssetValue"
          placeholder="请输入资产原值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="QUANTITY">
        <el-input
          v-model="queryParams.QUANTITY"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产净值" prop="netAssetValue">
        <el-input
          v-model="queryParams.netAssetValue"
          placeholder="请输入资产净值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计折旧" prop="accumulatedDepreciation">
        <el-input
          v-model="queryParams.accumulatedDepreciation"
          placeholder="请输入累计折旧"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际资产净值" prop="actualNetAssetValue">
        <el-input
          v-model="queryParams.actualNetAssetValue"
          placeholder="请输入实际资产净值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否盈亏" prop="isRendered">
        <el-input
          v-model="queryParams.isRendered"
          placeholder="请输入是否盈亏"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="责任人" prop="liablePersonId">
        <el-input
          v-model="queryParams.liablePersonId"
          placeholder="请输入责任人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="原因" prop="assetReason">
        <el-input
          v-model="queryParams.assetReason"
          placeholder="请输入原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存入地点" prop="depositeLocation">
        <el-input
          v-model="queryParams.depositeLocation"
          placeholder="请输入存入地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="assetState">
        <el-input
          v-model="queryParams.assetState"
          placeholder="请输入状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="OP">
        <el-input
          v-model="queryParams.OP"
          placeholder="请输入操作人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="opDate">
        <el-date-picker clearable
          v-model="queryParams.opDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择操作时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="规格" prop="SPECS">
        <el-input
          v-model="queryParams.SPECS"
          placeholder="请输入规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="MODEL">
        <el-input
          v-model="queryParams.MODEL"
          placeholder="请输入型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报废时间" prop="scrapDate">
        <el-date-picker clearable
          v-model="queryParams.scrapDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报废时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="运维团队" prop="operationsTeam">
        <el-input
          v-model="queryParams.operationsTeam"
          placeholder="请输入运维团队"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="专题" prop="subjectId">
        <el-input
          v-model="queryParams.subjectId"
          placeholder="请输入专题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="modelId">
        <el-input
          v-model="queryParams.modelId"
          placeholder="请输入设备类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="厂商" prop="manufactorId">
        <el-input
          v-model="queryParams.manufactorId"
          placeholder="请输入厂商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资源状态变更时间" prop="updateTime">
        <el-date-picker clearable
          v-model="queryParams.updateTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择资源状态变更时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="保修到期时间" prop="warrantyExpiration">
        <el-date-picker clearable
          v-model="queryParams.warrantyExpiration"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择保修到期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="使用人" prop="assetUserId">
        <el-input
          v-model="queryParams.assetUserId"
          placeholder="请输入使用人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="条型码" prop="barCode">
        <el-input
          v-model="queryParams.barCode"
          placeholder="请输入条型码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="序列号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入序列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['assets:assets:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['assets:assets:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['assets:assets:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['assets:assets:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资产ID" align="center" prop="assetId" />
      <el-table-column label="企业id" align="center" prop="corpId" />
      <el-table-column label="资产编号(GZ_当前时间戳)" align="center" prop="assetNo" />
      <el-table-column label="资产名称" align="center" prop="assetName" />
      <el-table-column label="分类(字典表 物业/公共)" align="center" prop="assetType" />
      <el-table-column label="使用部门" align="center" prop="deptId" />
      <el-table-column label="购入时间" align="center" prop="assetBuyDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assetBuyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="资产原值" align="center" prop="initialAssetValue" />
      <el-table-column label="数量" align="center" prop="QUANTITY" />
      <el-table-column label="资产净值" align="center" prop="netAssetValue" />
      <el-table-column label="累计折旧" align="center" prop="accumulatedDepreciation" />
      <el-table-column label="实际资产净值" align="center" prop="actualNetAssetValue" />
      <el-table-column label="是否盈亏" align="center" prop="isRendered" />
      <el-table-column label="责任人" align="center" prop="liablePersonId" />
      <el-table-column label="原因" align="center" prop="assetReason" />
      <el-table-column label="存入地点" align="center" prop="depositeLocation" />
      <el-table-column label="状态" align="center" prop="assetState" />
      <el-table-column label="操作人" align="center" prop="OP" />
      <el-table-column label="操作时间" align="center" prop="opDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.opDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="SPECS" />
      <el-table-column label="型号" align="center" prop="MODEL" />
      <el-table-column label="报废时间" align="center" prop="scrapDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scrapDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维护计划" align="center" prop="maintenancePlan" />
      <el-table-column label="运维团队" align="center" prop="operationsTeam" />
      <el-table-column label="专题" align="center" prop="subjectId" />
      <el-table-column label="设备类型" align="center" prop="modelId" />
      <el-table-column label="厂商" align="center" prop="manufactorId" />
      <el-table-column label="分类" align="center" prop="categoryId" />
      <el-table-column label="资源状态变更时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修到期时间" align="center" prop="warrantyExpiration" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.warrantyExpiration, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用人" align="center" prop="assetUserId" />
      <el-table-column label="条型码" align="center" prop="barCode" />
      <el-table-column label="序列号" align="center" prop="serialNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assets:assets:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['assets:assets:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物品管理-固定资产对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业id" prop="corpId">
          <el-input v-model="form.corpId" placeholder="请输入企业id" />
        </el-form-item>
        <el-form-item label="资产编号(GZ_当前时间戳)" prop="assetNo">
          <el-input v-model="form.assetNo" placeholder="请输入资产编号(GZ_当前时间戳)" />
        </el-form-item>
        <el-form-item label="资产名称" prop="assetName">
          <el-input v-model="form.assetName" placeholder="请输入资产名称" />
        </el-form-item>
        <el-form-item label="使用部门" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入使用部门" />
        </el-form-item>
        <el-form-item label="购入时间" prop="assetBuyDate">
          <el-date-picker clearable
            v-model="form.assetBuyDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择购入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="资产原值" prop="initialAssetValue">
          <el-input v-model="form.initialAssetValue" placeholder="请输入资产原值" />
        </el-form-item>
        <el-form-item label="数量" prop="QUANTITY">
          <el-input v-model="form.QUANTITY" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="资产净值" prop="netAssetValue">
          <el-input v-model="form.netAssetValue" placeholder="请输入资产净值" />
        </el-form-item>
        <el-form-item label="累计折旧" prop="accumulatedDepreciation">
          <el-input v-model="form.accumulatedDepreciation" placeholder="请输入累计折旧" />
        </el-form-item>
        <el-form-item label="实际资产净值" prop="actualNetAssetValue">
          <el-input v-model="form.actualNetAssetValue" placeholder="请输入实际资产净值" />
        </el-form-item>
        <el-form-item label="是否盈亏" prop="isRendered">
          <el-input v-model="form.isRendered" placeholder="请输入是否盈亏" />
        </el-form-item>
        <el-form-item label="责任人" prop="liablePersonId">
          <el-input v-model="form.liablePersonId" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="原因" prop="assetReason">
          <el-input v-model="form.assetReason" placeholder="请输入原因" />
        </el-form-item>
        <el-form-item label="存入地点" prop="depositeLocation">
          <el-input v-model="form.depositeLocation" placeholder="请输入存入地点" />
        </el-form-item>
        <el-form-item label="状态" prop="assetState">
          <el-input v-model="form.assetState" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="操作人" prop="OP">
          <el-input v-model="form.OP" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="操作时间" prop="opDate">
          <el-date-picker clearable
            v-model="form.opDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="规格" prop="SPECS">
          <el-input v-model="form.SPECS" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="型号" prop="MODEL">
          <el-input v-model="form.MODEL" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="报废时间" prop="scrapDate">
          <el-date-picker clearable
            v-model="form.scrapDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报废时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维护计划" prop="maintenancePlan">
          <el-input v-model="form.maintenancePlan" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="运维团队" prop="operationsTeam">
          <el-input v-model="form.operationsTeam" placeholder="请输入运维团队" />
        </el-form-item>
        <el-form-item label="专题" prop="subjectId">
          <el-input v-model="form.subjectId" placeholder="请输入专题" />
        </el-form-item>
        <el-form-item label="设备类型" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入设备类型" />
        </el-form-item>
        <el-form-item label="厂商" prop="manufactorId">
          <el-input v-model="form.manufactorId" placeholder="请输入厂商" />
        </el-form-item>
        <el-form-item label="分类" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入分类" />
        </el-form-item>
        <el-form-item label="保修到期时间" prop="warrantyExpiration">
          <el-date-picker clearable
            v-model="form.warrantyExpiration"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择保修到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用人" prop="assetUserId">
          <el-input v-model="form.assetUserId" placeholder="请输入使用人" />
        </el-form-item>
        <el-form-item label="条型码" prop="barCode">
          <el-input v-model="form.barCode" placeholder="请输入条型码" />
        </el-form-item>
        <el-form-item label="序列号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入序列号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssets, getAssets, delAssets, addAssets, updateAssets } from "@/api/assets/assets";

export default {
  name: "Assets",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物品管理-固定资产表格数据
      assetsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        corpId: null,
        assetNo: null,
        assetName: null,
        assetType: null,
        deptId: null,
        assetBuyDate: null,
        initialAssetValue: null,
        QUANTITY: null,
        netAssetValue: null,
        accumulatedDepreciation: null,
        actualNetAssetValue: null,
        isRendered: null,
        liablePersonId: null,
        assetReason: null,
        depositeLocation: null,
        assetState: null,
        OP: null,
        opDate: null,
        SPECS: null,
        MODEL: null,
        scrapDate: null,
        maintenancePlan: null,
        operationsTeam: null,
        subjectId: null,
        modelId: null,
        manufactorId: null,
        categoryId: null,
        updateTime: null,
        warrantyExpiration: null,
        assetUserId: null,
        barCode: null,
        serialNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        subjectId: [
          { required: true, message: "专题不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物品管理-固定资产列表 */
    getList() {
      this.loading = true;
      listAssets(this.queryParams).then(response => {
        this.assetsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assetId: null,
        corpId: null,
        assetNo: null,
        assetName: null,
        assetType: null,
        deptId: null,
        assetBuyDate: null,
        initialAssetValue: null,
        QUANTITY: null,
        netAssetValue: null,
        accumulatedDepreciation: null,
        actualNetAssetValue: null,
        isRendered: null,
        liablePersonId: null,
        assetReason: null,
        depositeLocation: null,
        assetState: null,
        OP: null,
        opDate: null,
        SPECS: null,
        MODEL: null,
        scrapDate: null,
        maintenancePlan: null,
        operationsTeam: null,
        subjectId: null,
        modelId: null,
        manufactorId: null,
        categoryId: null,
        updateTime: null,
        warrantyExpiration: null,
        assetUserId: null,
        barCode: null,
        serialNumber: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物品管理-固定资产";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assetId = row.assetId || this.ids
      getAssets(assetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物品管理-固定资产";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assetId != null) {
            updateAssets(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssets(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assetIds = row.assetId || this.ids;
      this.$modal.confirm('是否确认删除物品管理-固定资产编号为"' + assetIds + '"的数据项？').then(function() {
        return delAssets(assetIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('assets/assets/export', {
        ...this.queryParams
      }, `assets_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
